package com.teampower.idea.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.teampower.idea.action.builder.UsersBuilder;
import com.teampower.idea.base.BaseAction;
import com.teampower.idea.pojo.Users;
import com.teampower.idea.service.UsersService;
import com.teampower.idea.util.ToolsUtil;

public class UsersAction extends BaseAction {

	private String id;// 主键id
	private String username;// 登录名
	private String password;// 登录密码
	private String email;// 用户电子邮件
	private String question;// 登录错误提示
	private String answer;// 登录错误提示
	private String permission;// 管理权限
	private String inputdatetime;// 用户注册日期
	
	private String msg;// 登录错误提示
	
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getInputdatetime() {
		return inputdatetime;
	}
	public void setInputdatetime(String inputdatetime) {
		this.inputdatetime = inputdatetime;
	}
	public String getAnswer() {
		return answer;
	}
	public String getQuestion() {
		return question;
	}
	public void setQuestion(String question) {
		this.question = question;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	public String getUsername() {
		return username;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPermission() {
		return permission;
	}
	public void setPermission(String permission) {
		this.permission = permission;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}

	// 用户登录
	public String login() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("Entering 'UsersAction.login' method");
		}
		String result = SUCCESS;
		UsersService usersService = (UsersService) getBean("usersService");
		Users users = null;
		users = usersService.load(this.username, this.password);
		if (null != users) {
			// 获取ActionContext实例，通过它来访问Servlet API
			ActionContext context = ActionContext.getContext();
			context.getSession().put("users", users);
		} else {
			setMsg("用户名或密码错!");
			result = LOGIN;
		}
		return result;
	}

	// 查询所有数据
	public void getAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("Entering 'TestUsersAction.getAll' method");
		}
		String json = "";
		UsersService usersService = (UsersService) getBean("usersService");
		int counts =  usersService.getCounts();//总记录数
		List list = usersService.getAll(start, limit);
		json = UsersBuilder.getAll(list,counts);
		setJson(json);
		write();
	}
	
	//普通用户注册,权限为1,禁用用户权限为0
	public String reg() throws Exception{
		String result = SUCCESS;
		UsersService usersService = (UsersService) getBean("usersService");
		Users users = null;
		users = usersService.load(getUsername());
		if (null != users) {
			setMsg("该用户名已存在,不能重复注册!");
			result = "reg";
		} else {
			Users u = new Users();
			u.setUsername(this.username);
			u.setPassword(this.password);
			u.setEmail(this.email);
			u.setQuestion(this.question);
			u.setAnswer(this.answer);
			u.setInputdatetime(ToolsUtil.formatDate("yyyyMMddHHmmssSSS"));
			u.setPermission("1");
			usersService.save(u);
			setMsg("恭喜您注册成功,请您登陆。");
			result = LOGIN;
		}
		return result;
	}
	
}

